<template>
  <el-collapse-item name="3">
    <template #title>
      <h2>电池 配置</h2>
    </template>
    <div v-if="config?.server">
      <h3>主动均衡参数</h3>
      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="8">
          <b>开启压差</b>
        </el-col>
        <el-col :span="10">
          <el-input-number
            v-model="config.server.set_config.aes.start"
            style="width: 100%"
            :precision="3"
            :step="0.01"
            :min="0"
          />
        </el-col>
        <el-col :span="4" :offset="2">
          <b>V</b>
        </el-col>
      </el-row>

      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="8">
          <b>关闭压差</b>
        </el-col>
        <el-col :span="10">
          <el-input-number
            v-model="config.server.set_config.aes.end"
            style="width: 100%"
            :precision="3"
            :step="0.01"
            :min="0"
          />
        </el-col>
        <el-col :span="4" :offset="2">
          <b>V</b>
        </el-col>
      </el-row>

      <el-divider></el-divider>
      <h3>电池参数</h3>
      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="8">
          <b>电池个数</b>
        </el-col>
        <el-col :span="10">
          <el-input-number
            v-model="config.server.set_config.afe.bat_num"
            style="width: 100%"
            :step="1"
            :min="3"
            :max="6"
          />
        </el-col>
      </el-row>

      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="8">
          <b>电池容量</b>
        </el-col>
        <el-col :span="10">
          <el-input-number
            v-model="config.server.set_config.mAH"
            style="width: 100%"
            :step="1"
            :min="0"
          />
        </el-col>
        <el-col :span="4" :offset="2">
          <b>mAH</b>
        </el-col>
      </el-row>

      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="8">
          <b>最高电压</b>
        </el-col>
        <el-col :span="10">
          <el-select-v2
            v-model="config.server.set_config.afe.ovt"
            :options="bat_ovt_list"
            placeholder="请选择最大电压"
            size="large"
            style="width: 100%"
          />
        </el-col>
        <el-col :span="4" :offset="2">
          <b>V</b>
        </el-col>
      </el-row>

      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="8">
          <b>最低电压</b>
        </el-col>
        <el-col :span="10">
          <el-select-v2
            v-model="config.server.set_config.afe.uvt"
            :options="bat_uvt_list"
            placeholder="请选择最小电压"
            size="large"
            style="width: 100%"
          />
        </el-col>
        <el-col :span="4" :offset="2">
          <b>V</b>
        </el-col>
      </el-row>

      <el-row align="middle" style="margin-top: 10px">
        <el-col :span="10" :offset="1">
          <el-text type="info"
            >AFE总线过压:
            {{
              (
                bat_ovt_list[config.server.set_config.afe.ovt].voltage *
                config.server.set_config.afe.bat_num
              ).toFixed(3)
            }}
            V
          </el-text>
        </el-col>
        <el-col :span="10" :offset="2">
          <el-text type="info"
            >AFE总线欠压:
            {{
              (
                bat_uvt_list[config.server.set_config.afe.uvt].voltage *
                config.server.set_config.afe.bat_num
              ).toFixed(3)
            }}
            V
          </el-text>
        </el-col>
      </el-row>
      <el-button
        type="primary"
        style="margin-top: 10px; width: 90%"
        :loading="server_api.ticks?.afe_update_config > 0"
        :disabled="!change"
        @click="server_api.afe_update_config(config.server.set_config)"
      >
        保存
      </el-button>
    </div>
  </el-collapse-item>
</template>
<script setup lang="ts">
import store from "~/utils/store";
import server_api from "~/utils/server_api";
const config = toRef(store);
const change = computed(() => {
  return (
    JSON.stringify(config.value.server.set_config) !=
    JSON.stringify(config.value.server.user_config)
  );
});
/* 单节电池过压列表 */
const bat_ovt_list = Array.from({ length: 256 }).map((_, i) => {
  let value = (i * 0.006 + 3.199).toFixed(3);
  return { value: i, voltage: value, label: value.toString() };
});
/* 单节电池欠压列表 */
const bat_uvt_list = Array.from({ length: 256 }).map((_, i) => {
  let value = (i * 0.006 + 1.898).toFixed(3);
  return { value: i, voltage: value, label: value.toString() };
});
</script>
<style scoped></style>
